20180407

*事前準備:

a)安裝BeautifulSoup,爬蟲模組 (安裝方法: pip3 install bs4)

b)安裝lxml,Python內建庫裏頭解碼html的解析器 (安裝方法: pip3 intall lxml)

由於使用BS須將html原始碼包含進去,

先用Pyhton內建庫的方法,將url的原始碼讀取出來,如網頁包含中文則需decode。

from urllib.request import urlopen
html=urlopen("https://histock.tw/index/SFC").read().decode('utf-8')

導入BS的模組,

給定BS一個html以及解析方式,

然後印出網頁(html)裡h3的部分。

from bs4 import BeautifulSoup
soup=BeautifulSoup(html, features='lxml')
print (soup.h3)

results matching ""

    No results matching ""